package cn.tedu.dao.impl;

import java.util.List;

import cn.tedu.dao.UserDao;
import cn.tedu.dao.common.CommonDao;
import cn.tedu.entity.User;
import cn.tedu.vo.CountVO;
/**
 * 是UserDao接口mysql实现
 * @author Administrator
 *
 */
public class UserDaoImpl implements UserDao {

	@Override 
	public int login(String uname, String upassword)throws Exception {
		int id=0;
		String sql="select id from t_user where username=? and userpassword=?";
		Object[] params=new Object[] {
				uname,
				upassword
		};
		List<User> users=CommonDao.executeQuery(User.class, sql, params);
		if(users!=null && users.size()==1) {
			id=users.get(0).getId();
		}
		return id;
	}

	@Override
	public int addUser(User user)throws Exception {
		int rowAffect=0;
		String sql="insert into t_user(username,userpassword,age,address,head)values(?,?,?,?,?)";
		Object[] params=new Object[] {
				user.getName(),
				user.getPassword(),
				user.getAge(),
				user.getAddress(),
				user.getHead()
		};
		rowAffect=CommonDao.executeUpdate(sql, params);
		return rowAffect;
	}

	@Override
	public List<User> findAllUser()throws Exception {
		List<User> users=null;
		String sql="select id,username name,userpassword password,age,address,head from t_user";
		users=CommonDao.executeQuery(User.class, sql);
		return users;
	}

	@Override
	public int deleteUser(int id) throws Exception {
		int rowAffect=0;
		String sql="delete from t_user where id=?";
		rowAffect=CommonDao.executeUpdate(sql, id);
		return rowAffect;
	}

	@Override
	public User findUserById(int id) throws Exception {
		User user=null;
		String sql="select id,username name,userpassword password,age,address,head from t_user where id=?";
		List<User> users=CommonDao.executeQuery(User.class, sql, id);
		if(users!=null && users.size()==1) {
			user=users.get(0);
		}
		return user;
	}

	@Override
	public int updateUser(User user)throws Exception {
		int rowAffect=0;
		String sql="update t_user set username=?,userpassword=?,age=?,address=? where id=?";
		rowAffect=CommonDao.executeUpdate(sql, user.getName(),user.getPassword(),user.getAge(),user.getAddress(),user.getId());
		return rowAffect;
	}

	@Override
	public int getCount(String[] keywords)throws Exception {
		int count=0;
		String sql="select count(id) geshu from t_user where username like ? and address like ?";
		Object[] params=new Object[] {
				"%"+keywords[0]+"%",
				"%"+keywords[1]+"%"
		};
		List<CountVO> countVOs=CommonDao.executeQuery(CountVO.class, sql, params);
		if(countVOs!=null &&countVOs.size()==1) {
			count=countVOs.get(0).getGeshu();
		}
		return count;
	}

	@Override
	public List<User> getUsersByPage(int currentPage, int pageSize, String[] keywords)throws Exception {
		List<User> users=null;
		String sql="select id,username name,userpassword password,age,address,head from t_user "
				+ "where username like ? and address like ? "
				+ "limit ?,?";
		Object[] params=new  Object[] {
				"%"+keywords[0]+"%",
				"%"+keywords[1]+"%",
				(currentPage-1)*pageSize,
				pageSize
		};
		users=CommonDao.executeQuery(User.class, sql, params);
		return users;
	}

	@Override
	public int findUserByName(String uname) throws Exception {
		int id=0;
		String sql="select id from t_user where username=?";
		List<User> users=CommonDao.executeQuery(User.class, sql, uname);
		if(users!=null && users.size()==1) {
			id=users.get(0).getId();
		}
		return id;
	}

}
